package com.thti.mecool.client.activity;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.model.biz.SynchroBizImpl;
import com.thti.mecool.client.util.ImageUtil;
import com.thti.mecool.client.util.Util;

public class ShowCameraActivity extends BaseActivity {
	private ImageView imageView;
	private Button cancelBtn,saveBtn;
	String path;
	Context mContext;
	Bitmap camreaBitmap;
	SynchroBizImpl dbOperation;
	@SuppressWarnings("static-access")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.mecool_show_photo);
		Cache.add(this);
		dbOperation=new SynchroBizImpl(this);
		imageView=(ImageView)findViewById(R.id.mecool_show_photo_imageview);
		cancelBtn=(Button)findViewById(R.id.mecool_show_photo_cancel);
		saveBtn=(Button)findViewById(R.id.mecool_show_photo_save);
		cancelBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		saveBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				dbOperation.insertImagePath(saveBitmapToSdcard(camreaBitmap).trim());
				dbOperation.insertImagePath(saveBitmapToSdcard(ImageUtil.ResizeBitmap(camreaBitmap, 960)).trim());
				Toast.makeText(ShowCameraActivity.this, "保存成功", 5000).show();
				finish();
			}
		});
		final Animation anim=AnimationUtils.loadAnimation(this, R.anim.back);
        anim.setFillAfter(true);
		imageView.setImageBitmap(camreaBitmap);
		imageView.setAnimation(anim);
	}
		
    
	public String saveBitmapToSdcard(Bitmap bitmap){
		String sdStatus=Environment.getExternalStorageState();
		File file=null;
		if(!sdStatus.equals(Environment.MEDIA_MOUNTED)){
			Util.print("SD card is not available.");
			return null;
		}
		String path=Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"com.thti.mecool";
		File dir=new File(path);
		if(!dir.exists()){
			dir.mkdirs();
		}
		if(dir.exists()){
			file=new File(dir,System.currentTimeMillis()+".jpg");
		}
		try {
			BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(file));
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
//			bitmap.compress(Bitmap.CompressFormat.JPEG, 30, bos);
//			bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bos);
			bos.flush(); 
			bos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return file.getAbsolutePath();
	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(camreaBitmap!=null&&!camreaBitmap.isRecycled()){
			camreaBitmap.recycle();
			camreaBitmap=null;
		}
	}
    
	
    
}
